package com.alvis.media.configuration.spring.mvc;

import com.alvis.media.configuration.property.SystemConfig;
import com.alvis.media.configuration.spring.wx.TokenHandlerInterceptor;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;



@Configuration
@AllArgsConstructor
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    private final TokenHandlerInterceptor tokenHandlerInterceptor;
    private final SystemConfig systemConfig;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/", "/student/index.html");
        registry.addRedirectViewController("/student", "/student/index.html");
        registry.addRedirectViewController("/admin", "/admin/index.html");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 静态资源配置,添加更高优先级
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/static/")
                .setCachePeriod(31556926)
                .resourceChain(true);
        
        registry.addResourceHandler("/admin/static/**")
                .addResourceLocations("classpath:/static/admin/static/")
                .setCachePeriod(31556926)
                .resourceChain(true);
                
        registry.addResourceHandler("/student/static/**")
                .addResourceLocations("classpath:/static/student/static/")
                .setCachePeriod(31556926)
                .resourceChain(true);
        
        // HTML页面配置
        registry.addResourceHandler("/admin/**")
                .addResourceLocations("classpath:/static/admin/")
                .setCachePeriod(31556926);
                
        registry.addResourceHandler("/student/**")
                .addResourceLocations("classpath:/static/student/")
                .setCachePeriod(31556926);
        
        // 配置上传文件的访问路径
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("file:uploads/");
        
        // 配置分析结果的访问路径
        registry.addResourceHandler("/analysis/**")
                .addResourceLocations("file:uploads/analysis/");
        
        // 配置AI分析图片的访问路径
        registry.addResourceHandler("/ai-analysis/**")
                .addResourceLocations("file:D:/传媒数据管理/mediaAnalysisSystem/uploads/ai-analysis/");
        
        // 配置豆瓣Top250可视化图片路径
        registry.addResourceHandler("/douban-analysis/**")
                .addResourceLocations("file:uploads/ai-analysis/");
        
        // 其他静态资源
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .setCachePeriod(31556926);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> securityIgnoreUrls = systemConfig.getWx().getSecurityIgnoreUrls();
        String[] ignores = new String[securityIgnoreUrls.size()];
        registry.addInterceptor(tokenHandlerInterceptor)
                .addPathPatterns("/api/wx/**")
                .excludePathPatterns(securityIgnoreUrls.toArray(ignores));
        super.addInterceptors(registry);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedMethods("*")
                .allowedOrigins("*")
                .allowedHeaders("*");
        super.addCorsMappings(registry);
    }

}
